package sample;


import javafx.event.EventHandler;
import javafx.geometry.Rectangle2D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Color;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import utils.fileHandler.FileHandler;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

public class CantSee extends Thread{
    public static final double interval = 6;
    private static Stage stage = new Stage();
    private static Group group = new Group();
    private static Scene scene = new Scene(group);
    public static ArrayList<AppFrame> frameList = new ArrayList<AppFrame>();
    private static Rectangle2D screenRectangle = Screen.getPrimary().getVisualBounds();
    private static final Rectangle2D rectangle = new Rectangle2D(0,0,screenRectangle.getWidth()/4,screenRectangle.getHeight()/4);
    private static String chooseWindow = null;
    private static AppFrame chooseFrame = null;
    private static MouseMoveListener mouseMoveListener = new MouseMoveListener();
    public static String root;
    public CantSee() {
        super();
        stage.setMinHeight(screenRectangle.getHeight());
        stage.setMinWidth(screenRectangle.getWidth());
        stage.setTitle("cantsee");
        group.setStyle("-fx-background:transparent;");
        group.setMouseTransparent(true);
        scene.setFill(Color.TRANSPARENT);
        root= FileHandler.getRootPath()+"source/";
        stage.setScene(scene);
        stage.initStyle(StageStyle.TRANSPARENT);
    }
    public static void adapteScreen(){
        screenRectangle = Screen.getPrimary().getVisualBounds();
        stage.setMinHeight(screenRectangle.getHeight());
        stage.setMinWidth(screenRectangle.getWidth());

    }
    public static void hideFrame(){
        stage.hide();
        chooseFrame = null;
        for(int i = 0; i < frameList.size();i ++){
            frameList.get(i).rectangle.setVisible(false);
        }
        frameList.clear();
    }
    public static void show(){
        stage.show();
    }

    public static Stage getStage(){
        return stage;
    }

    public static String getChooseWindow(){
        return chooseWindow;
    }
    public static void  setChooseWindow(String ws){
        chooseWindow = ws;
    }

    public static AppFrame getChooseFrame(){
        return chooseFrame;
    }
    public static void setChooseFrame(AppFrame af){
        chooseFrame = af;
    }
    public static void setChooseFrame(int i){
        chooseFrame = frameList.get(i);
    }

    public static double[] getFrameRectangle(int i){
        double []rectangle = new double[4];
        AppFrame af = frameList.get(i);
        rectangle[0] = af.rectangle.getX() - interval;
        rectangle[1] = af.rectangle.getY() - interval;
        rectangle[2] = af.rectangle.getX() + af.rectangle.getWidth() + interval;
        rectangle[3] = af.rectangle.getY() + af.rectangle.getHeight() + interval;
        return rectangle;
    }
    public static int getFrameListSize(){
        return frameList.size();
    }

    public static void addFrame(double startx,double starty,double endx,double endy){
        frameList.add(new AppFrame());
        frameList.get(frameList.size()-1).addFrameTo(group);
        frameList.get(frameList.size()-1).setFrame(startx + interval,starty + interval,endx - 2*interval,endy - 2*interval);
    }

    public static Rectangle2D getRectangle(){
        return rectangle;
    }
    public static Rectangle2D getScreenRectangle(){
        return screenRectangle;
    }
    public static void releaseAllFrame(){
        for(int i = 0;i < frameList.size();i ++){
            frameList.get(i).setColor(Color.rgb(255,255,0));
        }
    }

    public static String DrawImage(String root, String name, List<AppFrame> frameList) throws Exception{
        String path = root + name + ".png";
        File file = new File(path);
        BufferedImage image = new BufferedImage(280, 150, Transparency.TRANSLUCENT);
        Graphics2D g2d = image.createGraphics();
        g2d.setStroke(new BasicStroke(4));
//        image = g2d.getDeviceConfiguration().createCompatibleImage(400, 300, Transparency.TRANSLUCENT);
//        g2d = image.createGraphics();
        g2d.setColor(java.awt.Color.YELLOW);
        for(int i = 0;i < frameList.size();i ++){
            AppFrame af = frameList.get(i);
            g2d.drawRect((int)(af.rectangle.getX()*280/1920),(int)(af.rectangle.getY()*150/1080),(int)(af.rectangle.getWidth()*280/1920),(int)(af.rectangle.getHeight()*150/1080));

        }
        g2d.dispose();
        ImageIO.write(image,"png",file);
        return path;
    }

    public static String CreateScreenFile(String root, String name ,List<AppFrame> frameList) throws Exception{
        String path = root + name + ".txt";
        File file = new File(path);
        PrintStream ps = new PrintStream(file);
        for(int i = 0;i < frameList.size();i ++){
            AppFrame af = frameList.get(i);
            ps.println(af.rectangle.getX()/4 + "#" + af.rectangle.getY()/4 + "#" + af.rectangle.getWidth()/4 + "#" + af.rectangle.getHeight()/4);

        }
        ps.close();
        return path;
    }

    public static boolean deleteScreenInfo(String root, String name){
        String path = root + name + ".txt";
        File file = new File(path);
        if(!file.delete()){
            System.out.println("分屏文件刪除失敗！");
            return false;
        }
        path = root + name + ".png";
        file = new File(path);
        if(!file.delete()){
            System.out.println("分屏圖片刪除失敗！");
            return false;
        }
        return true;
    }

    static class MouseMoveListener implements EventHandler<MouseEvent>{

        @Override
        public void handle(MouseEvent mouseEvent) {
            for(int i = 0;i < frameList.size();i ++){

                if(mouseEvent.getX() > frameList.get(i).rectangle.getX() && mouseEvent.getX() < frameList.get(i).rectangle.getX() + frameList.get(i).rectangle.getWidth()
                        && mouseEvent.getY() > frameList.get(i).rectangle.getY() && mouseEvent.getY() < frameList.get(i).rectangle.getY() + frameList.get(i).rectangle.getHeight()) {
                    if (chooseFrame != null){
                        chooseFrame.setColor(Color.rgb(255 ,255 ,0));
                        chooseFrame.rectangle.setViewOrder(2);
                    }

                    chooseFrame = frameList.get(i);
                    chooseFrame.setColor(Color.rgb(0 ,205 ,0));
                    chooseFrame.rectangle.setViewOrder(0);
                }
            }
        }
    }

}
